<?php
    include('checkAdminAccess.php');
    $iniVars = parse_ini_file('../../config.ini',TRUE);
    include('../classes/adodb5/adodb.inc.php');
    include('../classes/class.upload.php');
    include('../function/db.func.php');
    
    $action = $_POST['action'];
    
    if($action=='upload_img')
    {
        if(isset($_FILES['promoImg']) AND $_FILES['promoImg']['name']!='')
        {
            $error = FALSE;
            $handle = new Upload($_FILES['promoImg']);
            
            $oldImg = $iniVars['Locations']['sitepath'].$_POST['oldpath'];
                
            if( $handle->uploaded )
            {
                $handle->image_resize = true;
                $handle->image_x = 560;
                $handle->image_ratio_y = true;
                $handle->Process($iniVars['Locations']['sitepath'].'images/promotion');
                $sql = 'UPDATE promotion SET img_path="images/promotion/'.$handle->file_dst_name.'"';
                $result = $db->Execute($sql);
                if($result)
                {
                    unlink($oldImg);
                    $msg = "<br />อัพโหลดรูปภาพเรียบร้อยแล้ว.";
                }
                else
                {
                    $error = TRUE;
                    unlink($iniVars['Locations']['sitepath'].$handle->file_dst_name);
                    $msg = "<br />ไม่สามารถอัพโหลดรูปภาพได้ กรุณาลองใหม่อีกครั้ง.";
                }
         
            }
            
            if($error)
            {
                 echo <<<js
                <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
                <script type="text/javascript">
                    $(".submit_img_indicator", top.document).css('display','none');
                    $("#img_error_msg", top.document).html('{$msg}');
                    $("#img_error_msg", top.document).css('display','block');
                    setInterval("top.location='../index.php?page=home&part=promotion';",3000);
                </script>
js;
            }
            else
            {
                 echo <<<js
                <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
                <script type="text/javascript">
                    $(".submit_img_indicator", top.document).css('display','none');
                    $("#img_success_msg", top.document).html('{$msg}');
                    $("#img_success_msg", top.document).css('display','block');
                    setInterval("top.location='../index.php?page=home&part=promotion';",3000);
                </script>
js;
            }
        }
    }
    else if($action=='upload_file')
    {
        if(isset($_FILES['promoFile']) AND $_FILES['promoFile']['name']!='')
        {
            $error = FALSE;
            $handle = new Upload($_FILES['promoFile']);
            
            if($handle->file_src_size <= 2097152)
            {
                if($handle->file_src_name_ext=='pdf')
                {
                
                    $oldFile = $iniVars['Locations']['sitepath'].$_POST['oldfilepath'];
                    
                    if( $handle->uploaded )
                    {
                 
                        $handle->Process($iniVars['Locations']['sitepath'].'files/promotion');
    
                        $sql = 'UPDATE promotion SET file_path="files/promotion/'.$handle->file_dst_name.'"';
                        $result = $db->Execute($sql);
                        if($result)
                        {
                            if($_POST['oldfilepath']!='' AND file_exists($oldFile)){
                                unlink($oldFile);
                            }
                            $msg = "<br />อัพโหลดไฟล์เรียบร้อยแล้ว.";
                        }
                        else
                        {
                            $error = TRUE;
                            unlink($iniVars['Locations']['sitepath'].$handle->file_dst_name);
                            $msg = "<br />ไม่สามารถอัพโหลดไฟล์ได้ กรุณาลองใหม่อีกครั้ง.";
                        }
                 
                    }
                }
                else
                {
                    $error = TRUE;
                    $msg = "<br />ยอมรับไฟล์นามสกุล PDF เท่านั้น"; 
                }
            }
            else
            {
                $error = TRUE;
                $msg = "<br />ขนาดไฟล์ต้องไม่เกิน 2 MB"; 
            }
            
            
            if($error)
            {
                 echo <<<js
                <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
                <script type="text/javascript">
                    $(".submit_file_indicator", top.document).css('display','none');
                    $("#file_error_msg", top.document).html('{$msg}');
                    $("#file_error_msg", top.document).css('display','block');
                    setInterval("top.location='../index.php?page=home&part=promotion';",3000);
                </script>
js;
            }
            else
            {
                 echo <<<js
                <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
                <script type="text/javascript">
                    $(".submit_file_indicator", top.document).css('display','none');
                    $("#file_success_msg", top.document).html('{$msg}');
                    $("#file_success_msg", top.document).css('display','block');
                    setInterval("top.location='../index.php?page=home&part=promotion';",3000);
                </script>
js;
            }
        }
    }
    else if($action=='delete_file')
    {
        $sql = 'UPDATE promotion SET file_path=""';
        $result = $db->Execute($sql);
        if($result)
        {
            $oldFile = $iniVars['Locations']['sitepath'].$_POST['oldfilepath'];
            unlink($oldFile);
            echo 'success';
        }
        else
        {
            echo 'fail';
        }
    }
?>